﻿/*
* Copyright (c) 2008 michiyasu wada
* http://www.seyself.com/
* 
* Distributed under The MIT License.
* [http://www.opensource.org/licenses/mit-license.php]
*/

//****************************************************************************
// ActionScript Standard Library
// flash.geom.Point object
//****************************************************************************

class flash7.geom.Point
{
    public var x:Number = 0;
    public var y:Number = 0;
    
    public function get length():Number
    {
        return Math.sqrt( x * x + y * y );
    }
    
    public static function interpolate( pt1:Point, pt2:Point, f:Number ):Point
    {
        var nx:Number = pt2.x - pt1.x;
        var ny:Number = pt2.y - pt1.y;
        var angle:Number = Math.atan2( ny , nx );
        var dis:Number = Math.sqrt( nx * nx + ny * ny ) * f;
        var sx:Number = pt2.x - Math.cos( angle ) * dis;
        var sy:Number = pt2.y - Math.sin( angle ) * dis;
        return new Point( sx , sy );
    }
    
    public static function distance( pt1:Point, pt2:Point ):Number
    {
        var nx:Number = pt2.x - pt1.x;
        var ny:Number = pt2.y - pt1.y;
        return Math.sqrt( nx * nx + ny * ny );
    }
    
    public static function polar( len:Number, angle:Number ):Point
    {
        var nx:Number = Math.cos( angle ) * len;
        var ny:Number = Math.sin( angle ) * len;
        return new Point( nx , ny );
    }

    public function Point( x:Number, y:Number )
    {
        this.x = ( isNaN(x) ) ? 0 : x;
        this.y = ( isNaN(y) ) ? 0 : y;
        this["add"] = this.addition;
    }
    
    public function clone():Point
    {
        return new Point( x , y );
    }
    
    public function offset( dx:Number, dy:Number ):Void
    {
        x += dx;
        y += dy;
    }
    
    public function equals( toCompare:Object ):Boolean
    {
        return ( toCompare.x==x && toCompare.y==y );
    }
    
    public function subtract( v:Point ):Point
    {
        return new Point( x - v.x , y - v.y );
    }
    
    public function addition( v:Point ):Point
    {
        return new Point( x + v.x , y + v.y );
    }
    
    public function normalize( length:Number ):Void
    {
        var angle:Number = Math.atan2( y , x );
        this.x = Math.cos( angle ) * length;
        this.y = Math.sin( angle ) * length;
    }
    
    public function toString():String
    {
        return "(x=" + x + ", y=" + y + ")";
    }
}